<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2017/12/23
 * Time: 下午4:58
 */

namespace Biz\module\Classes\adm;

use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\module\Classes\core\SearchListAbstract;
use Doraemon\model\Member as mMember;
use Medoo\Medoo;


final class OrderRefundSearch extends SearchListAbstract
{
    private $join = [];

    protected function buildSelect()
    {
        $this->select = '*';
    }

    protected function buildWhere()
    {
        //状态
        if (isset($this->params['type_refund']) && $this->params['type_refund'] > 0) {
            $this->where['type_refund'] = $this->params['type_refund'];
        }

        if (isset($this->params['status']) && $this->params['status'] > 0) {
            $this->where['status'] = $this->params['status'];
        }

        if (isset($this->params['reason']) && $this->params['reason'] > 0) {
            $this->where['reason'] = $this->params['reason'];
        }

        if (isset($this->params['refund_order_no']) && $this->params['refund_order_no']) {
            $this->where['refund_order_no'] = $this->params['refund_order_no'];
        }

        if (isset($this->params['order_no']) && $this->params['order_no']) {
            $this->where['order_no'] = $this->params['order_no'];
        }

        if (isset($this->params['express_no']) && $this->params['express_no']) {
            $this->where['express_no'] = $this->params['express_no'];
        }

        if (isset($this->params['start_date']) && isset($this->params['stop_date']) && $this->params['start_date'] && $this->params['stop_date']) {
            $this->where['addtime[<>]'] = [
                $this->params['start_date'],
                date('Y-m-d', strtotime($this->params['stop_date']) + 86400)
            ];
        }

        if (isset($this->params['succ_start_date']) && isset($this->params['succ_stop_date']) && $this->params['succ_start_date'] && $this->params['succ_stop_date']) {
            $this->where['status'] = mOrderRefund::STATUS_COMPLETE;
            $this->where['confirm_time[<>]'] = [
                $this->params['succ_start_date'],
                date('Y-m-d', strtotime($this->params['succ_stop_date']) + 86400)
            ];
        }


        //自定义搜索条件1
        if (!empty($this->params['field1'])) {
            if (!empty($this->params['keyword1'])) {
                if (in_array($this->params['field1'], [
                    'uid',
                    'shop_id'
                ])) {
                    $this->where['' . $this->params['field1']] = $this->params['keyword1'];
                }
                if ($this->params['field1'] == 'shop_name') {
                    $shopInfo = mShopBasicInfo::getInfoByName($this->params['keyword1']);

                    if (isset($shopInfo['shop_id'])) {
                        $this->where['shop_id'] = $shopInfo['shop_id'];
                    }
                }
                if ($this->params['field1'] == 'nickname') {
                    $uid = mMember::getUidByNick($this->params['keyword1']);

                    if ($uid > 0) {
                        $this->where['uid'] = $uid;
                    }
                }
            }
        }


    }

    public function getCount()
    {

        if ($this->join) {
            $count = mOrderRefund::count($this->join, "*", $this->where);
        } else {
            $count = mOrderRefund::count("*", $this->where);
        }


        return $count;
    }

    public function getList()
    {
        if ($this->join) {
            $list = mOrderRefund::select($this->join, $this->select, array_merge($this->where, [
                'ORDER' => Medoo::raw('addtime desc'),
                'LIMIT' => $this->limit
            ]));
        } else {
            $list = mOrderRefund::select($this->select, array_merge($this->where, [
                'ORDER' => Medoo::raw('addtime desc'),
                'LIMIT' => $this->limit
            ]));
        }

        if ($list) {

            $orderNoList = array_column($list, 'order_no');
            $orderList = mOrder::getOrderInfoList(['order_no' => $orderNoList], [], [], true);

            $orderList = array_column($orderList, null, 'order_no');

            if ($list) {
                foreach ($list as &$info) {
                    mOrderRefund::formatInfo($info, 'adm');

                    $info['order_status'] = $orderList[$info['order_no']]['status'];
                    $info['order_status_desc'] = $orderList[$info['order_no']]['status_desc'];

                    $info['order_goods'] = [
                        'sku_desc' => $info['sku_desc'],
                        'title' => $info['title'],
                        'cover_img' => $info['cover_img_thumb_url'],
                        'cover_img_big' => $info['cover_img_url'],
                    ];
                }
            }

            return $list;
        }
    }

}
